package com.sinochem.yunlian.ship.base.rest.app;

import com.alibaba.fastjson.JSONObject;
import com.sinochem.yunlian.ship.base.biz.BsDataDictItemBiz;
import com.sinochem.yunlian.ship.base.entity.BsDataDictItem;
import com.sinochem.yunlian.ship.base.vo.response.DictItemVo;
import com.sinochem.yunlian.ship.common.annotation.IgnoreToken;
import com.sinochem.yunlian.ship.common.constant.RedisKey;
import com.sinochem.yunlian.ship.common.msg.RestResponse;
import com.sinochem.yunlian.ship.common.rest.BaseController;
import com.sinochem.yunlian.ship.common.util.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author panyanan
 * @date 2017-11-20 21:02
 *
 * */
@IgnoreToken
@RestController
@RequestMapping("/m/dataDict")
public class MBsDataDictController extends BaseController<BsDataDictItemBiz,BsDataDictItem> {

    @Autowired
    private BsDataDictItemBiz bsDataDictItemBiz;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("findDictItemByCode/anon")
    public RestResponse findDictItemByCode(@RequestBody Map<String,Object> param) {
        String dictCode = param.get("dictCode").toString();
        List<DictItemVo> resultList =  new ArrayList<>();
        String key = String.format(RedisKey.KEY_DICT_ITEM_LIST_PREFIX,dictCode);
        if (redisTemplate.hasKey(key)) {
            String dictDatas =  (String) redisTemplate.opsForValue().get(key);
            resultList = JSONObject.parseArray(dictDatas,DictItemVo.class);
        } else {
            List<BsDataDictItem> items = bsDataDictItemBiz.findByDictCode(dictCode);
            for (BsDataDictItem item : items) {
                DictItemVo dictItemVo = new DictItemVo();
                dictItemVo.setId(item.getId());
                dictItemVo.setDictId(item.getDictId());
                dictItemVo.setDictCode(item.getDictCode());
                dictItemVo.setItemValue(item.getItemValue());
                dictItemVo.setItemName(item.getItemName());
                dictItemVo.setItemGroup(item.getItemGroup());
                dictItemVo.setShowOrder(item.getShowOrder());
                resultList.add(dictItemVo);

            }
            redisTemplate.opsForValue().set(key,JSONObject.toJSONString(resultList));
        }
        return RestResponse.success(resultList);
    }


    @RequestMapping("findDictItemByCodes/anon")
    public RestResponse findDictItemByCodes(@RequestBody Map<String,Object> param) {

        String dictCodes = param.get("dictCodes").toString();

        Map<String,Object> resultMap = new HashMap<>();
        for (String dictCode : dictCodes.split(",")) {
            List<DictItemVo> resultList =  new ArrayList<>();
            String key = String.format(RedisKey.KEY_DICT_ITEM_LIST_PREFIX,dictCode);
            if (redisTemplate.hasKey(key)) {
                String dictDatas =  (String) redisTemplate.opsForValue().get(key);
                resultList = JSONObject.parseArray(dictDatas,DictItemVo.class);
                resultMap.put(dictCode,resultList);
            } else {
                List<BsDataDictItem> items = bsDataDictItemBiz.findByDictCode(dictCode);
                for (BsDataDictItem item : items) {
                    DictItemVo dictItemVo = new DictItemVo();
                    dictItemVo.setId(item.getId());
                    dictItemVo.setDictId(item.getDictId());
                    dictItemVo.setDictCode(item.getDictCode());
                    dictItemVo.setItemValue(item.getItemValue());
                    dictItemVo.setItemName(item.getItemName());
                    dictItemVo.setShowOrder(item.getShowOrder());
                    dictItemVo.setItemGroup(item.getItemGroup());
                    resultList.add(dictItemVo);
                }
                redisTemplate.opsForValue().set(key,JSONObject.toJSONString(resultList));
                resultMap.put(dictCode,resultList);
            }
        }

        return RestResponse.success(resultMap);
    }


}